home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr01 / halcn305.zip / GSOB_GEN.PAS < prev    next >
Pascal/Delphi Source File  |  1993-04-17  |  11KB  |  373 lines

  1. Unit GSOB_Gen;
  2. {------------------------------------------------------------------------------
  3.                               DBase File Builder
  4.  
  5.        GSOB_GEN Copyright (c)  Richard F. Griffin
  6.  
  7.        10 August 1992
  8.  
  9.        102 Molded Stone Pl
  10.        Warner Robins, GA  31088
  11.  
  12.        -------------------------------------------------------------
  13.        This unit creates a test dBase file and adds records to an
  14.        existing test file.
  15.  
  16.        The unit uses two external files to generate data.  The first is
  17.        TESTDATA.FIL, which contains names and addresses the routines use
  18.        to randomly generate data.  The second file is WISDOM.FIL, which
  19.        contains one-liners used to randomly create memo fields.  Each of
  20.        these files may be modified to fit the user's requirements.
  21.  
  22.        File structure is:
  23.  
  24.               LASTNAME    C   30    0
  25.               FIRSTNAME   C   30    0
  26.               STREET      C   30    0
  27.               OFFICE      C   30    0
  28.               CITY        C   30    0
  29.               STATE       C   2     0
  30.               ZIP         C   10    0
  31.               TELEPHONE   C   20    0
  32.               BIRTHDATE   D    8    0
  33.               PAYMENT     N    9    2
  34.               PAIDFLAG    L    1    0
  35.               RANDOMNUM   N   12    5
  36.               UNIQUEID    C    8    0
  37.               COMMENTS    M   10    0   (Only included if Memo Field requested)
  38.  
  39.        The MakeTestData unit is called as follows for database creation:
  40.  
  41.           MakeTestData(typFile, namFile, numRecs, MemoAlso);
  42.  
  43.           where:
  44.                   typFile  = 3 for db3 or 4 for db4
  45.                   namFile  = file name without the file extension
  46.                   numRecs  = number of records to insert
  47.                   MemoAlso = boolean true to create a memo file and records,
  48.                              false to omit a memo field.
  49.  
  50.        The AddTestData unit is called as follows for additional records:
  51.  
  52.           AddTestData(namObjt, numRecs);
  53.  
  54.           where:
  55.                   namObjt  = Pointer to the GS_dBFld_Objt object for the file
  56.                   numRecs  = number of records to add
  57.  
  58. -------------------------------------------------------------------------------}
  59. interface
  60. {$O+}
  61.  
  62. uses
  63.    GSOB_Var,
  64.    GSOB_Dte,
  65.    GSOB_Str,
  66.    GSOB_DBF,
  67.    GSOB_DBS,
  68.    {$IFDEF WINDOWS}
  69.       WinCRT,
  70.       WinDOS,
  71.       Objects;
  72.    {$ELSE}
  73.       CRT,
  74.       DOS,
  75.       GSOB_Obj;
  76.    {$ENDIF}
  77.  
  78.  
  79. procedure MakeTestData(dbfType : integer; namFile : string;
  80.                        numRecs : integer; MemoAlso : boolean);
  81. procedure AddTestData(namObjt : GSP_dBHandler; numRecs : integer);
  82.  
  83. implementation
  84. const
  85.  
  86.    CollectionsEmpty : boolean = true;
  87.  
  88.    yearweight : array [0..6] of word
  89.               = (7665,14600,14600,23725,32850,40150,40150);
  90.    memoweight : array [0..7] of word
  91.               = (0,500,500,1000,1500,1000,500,500);
  92.  
  93.  
  94. var
  95.    fli : text;
  96.    flp : text;
  97.    s   : string;
  98.    i,
  99.    j,
  100.    k   : integer;
  101.    x   : integer;
  102.  
  103.    gfMemoArray : array[0..50] of word;
  104.    gfMemoLines : integer;
  105.    gfMemoBytes : longint;
  106.    gfMemoAvg : integer;
  107.    gfLastName : string[30];
  108.    gfFirstName : string[30];
  109.    gfStreet : string[80];
  110.    gfOffice : string;
  111.    gfState : string[2];
  112.    gfZip   : string[5];
  113.    gfCity  : string[30];
  114.    gfTelePhone : string[20];
  115.    gfBirthDate : string[8];
  116.    gfPayment : string[9];
  117.    gfPaidFlag : string[1];
  118.    gfRandomNum : string[12];
  119.    gfUniqueID  : string[8];
  120.    SZC    : string;
  121.  
  122.    MoColl,
  123.    LNColl,
  124.    FNColl,
  125.    StColl,
  126.    OfColl,
  127.    SZColl : TCollection;
  128.  
  129.    fx : GSP_DBFBuild;
  130.    t : string;
  131.  
  132.    dbx : GSP_dBHandler;
  133.    useMemo : boolean;
  134.  
  135.  
  136.  
  137. {  Use this when a consistent series of records needs to be created to
  138.    test a program.  Otherwise, comment it out.
  139. }
  140.  
  141. {
  142. Procedure Randomize;
  143. begin
  144.    RandSeed := 1234;
  145. end;
  146. }
  147.  
  148. procedure MakeCollections;
  149. begin
  150.    FileMode := 66;
  151.    assign(fli,'testdata.fil');
  152.    reset(fli);
  153.    LNColl.Init(32,32);
  154.    FNColl.Init(32,32);
  155.    StColl.Init(32,32);
  156.    OfColl.Init(32,32);
  157.    SZColl.Init(32,32);
  158.    x := 0;
  159.    readln(fli,s);
  160.    while not EOF(fli) do
  161.    begin
  162.       s := TrimR(s);
  163.       if s[1] = '%' then
  164.       begin
  165.          if s = '%LASTNAME' then x := 1;
  166.          if s = '%FIRSTNAME' then x := 2;
  167.          if s = '%STREET' then x := 3;
  168.          if s = '%OFFICE' then x := 4;
  169.          if s = '%STATEZIPCITY' then x := 5;
  170.       end
  171.       else
  172.       case x of
  173.          1 : LNColl.Insert(NewStr(s));
  174.          2 : FNColl.Insert(NewStr(s));
  175.          3 : StColl.Insert(NewStr(s));
  176.          4 : OfColl.Insert(NewStr(s));
  177.          5 : SZColl.Insert(NewStr(s));
  178.       end;
  179.       readln(fli,s);
  180.    end;
  181.    close(fli);
  182.    if not useMemo then exit;
  183.    gfMemoBytes := 0;
  184.    assign(fli,'wisdom.fil');
  185.    reset(fli);
  186.    MoColl.Init(2000,500);
  187.    readln(fli,s);
  188.    while not EOF(fli) do
  189.    begin
  190.       s := TrimR(s);
  191.       gfMemoBytes := gfMemoBytes + ord(s[0]);
  192.       MoColl.Insert(NewStr(s));
  193.       readln(fli,s);
  194.    end;
  195.    close(fli);
  196.    gfMemoAvg := gfMemoBytes div MoColl.Count;
  197. end;
  198.  
  199. Function RandString(l,h : integer) : string;
  200. var
  201.    v : integer;
  202.    g : string;
  203. begin
  204.    v := random((h-l)+1);
  205.    v := v + l;
  206.    str(v,g);
  207.    RandString := g;
  208. end;
  209.  
  210. procedure BuildRecordData;
  211. var
  212.    z   : string;
  213.    i1,
  214.    j1,
  215.    j2,
  216.    k1  : word;
  217.    i2  : longint;
  218.    tf  : boolean;
  219.    s1  : string[5];
  220. begin
  221.    j := random(LNColl.Count);
  222.    gfLastName := PString(LNColl.At(j))^;
  223.    j := random(FNColl.Count);
  224.    gfFirstName := PString(FNColl.At(j))^ + ' ' + chr(Random(26)+65) + '.';
  225.    j := random(StColl.Count);
  226.    gfStreet := RandString(10,9999) + ' ' + PString(StColl.At(j))^;
  227.    j := random(OfColl.Count*3);
  228.    if j < OfColl.Count then
  229.       gfOffice := PString(OfColl.At(j))^ + ' ' + RandString(1,99)
  230.    else gfOffice := '';
  231.    j := random(SZColl.Count);
  232.    s := PString(SZColl.At(j))^;
  233.    gfState := copy(s,1,2);
  234.    gfZip := copy(s,3,5);
  235.    gfCity := copy(s,8,30);
  236.    gfTelePhone := RandString(100,600) + ' ' + RandString(100,999) + '-' +
  237.                   RandString(1000,9999);
  238.    i1 := yearweight[random(7)];
  239.    i2 := random(i1)+1;
  240.    gfBirthDate := GS_Date_dBStor(GS_Date_Curr - i2);
  241.    i1 := random(20000) + 1;
  242.    str(i1:6,gfPayment);
  243.    gfPayment := gfPayment + '.' + RandString(10,99);
  244.    i1 := random(2);
  245.    if i1 = 0 then gfPaidFlag := 'F' else gfPaidFlag := 'T';
  246.  
  247.    i1 := random(2);
  248.    if i1 = 0 then gfRandomNum := '-' else gfRandomNum := '';
  249.    s1 := RandString(0,30000);
  250.    while length(s1) < 5 do s1 := s1+'0';
  251.    gfRandomNum := gfRandomNum + RandString(0,30000) + '.' + s1;
  252.    while length(gfRandomNum) < 12 do gfRandomNum := ' ' + gfRandomNum;
  253.    gfUniqueID := Unique_Field;
  254.    if not useMemo then exit;
  255.    dbx^.MemoFile^.MemoClear;
  256.    j2 := random(8);
  257.    j2 := memoweight[j2];
  258.    if j2 = 0 then exit;
  259.    s := '--- ' + gfLastName + ', ' + gfFirstName + ' Memo Record';
  260.    dbx^.MemoInsLine(0,s);
  261.    gfMemoLines := random(j2 div gfMemoAvg);
  262.    i1 := 0;
  263.    while i1 <= gfMemoLines do
  264.    begin
  265.       j1 := random(MoColl.Count);
  266.       tf := true;
  267.       if i1 > 0 then
  268.          for k1 := 0 to i1 do if j1 = gfMemoArray[k1] then tf := false;
  269.       if tf then
  270.       begin
  271.          s := PString(MOColl.At(j1))^;
  272.          dbx^.MemoInsLine(0,s);
  273.          gfMemoArray[i1] := j1;
  274.          inc(i1);
  275.       end;
  276.    end;
  277. end;
  278.  
  279. procedure MakeTestData(dbfType : integer; namFile : string;
  280.                        numRecs : integer; MemoAlso : boolean);
  281. begin
  282.    useMemo := MemoAlso;
  283.    if CollectionsEmpty then MakeCollections;
  284.    CollectionsEmpty := false;
  285.  
  286.            {Create new dBase file}
  287.    if dbfType = 3 then
  288.       fx := New(GSP_db3Build, Init(namFile))
  289.    else
  290.       fx := New(GSP_db4Build, Init(namFile));
  291.    fx^.InsertField('LASTNAME','C',30,0);
  292.    fx^.InsertField('FIRSTNAME','C',30,0);
  293.    fx^.InsertField('STREET','C',30,0);
  294.    fx^.InsertField('OFFICE','C',30,0);
  295.    fx^.InsertField('CITY','C',30,0);
  296.    fx^.InsertField('STATE','C',2,0);
  297.    fx^.InsertField('ZIP','C',10,0);
  298.    fx^.InsertField('TELEPHONE','C',20,0);
  299.    fx^.InsertField('BIRTHDATE','D',8,0);
  300.    fx^.InsertField('PAYMENT','N',9,2);
  301.    fx^.InsertField('PAIDFLAG','L',1,0);
  302.    fx^.InsertField('RANDOMNUM','N',12,5);
  303.    fx^.InsertField('UNIQUEID','C',8,0);
  304.    if useMemo then fx^.InsertField('COMMENTS','M',10,0);
  305.    Dispose(fx, Done);
  306.  
  307.            {Add records to the file}
  308.  
  309.    New(dbx, Init(namFile));
  310.    dbx^.Open;
  311.    randomize;
  312.    dbx^.StatusUpdate(StatusStart,GenFStatus,numRecs);
  313.    for i := 1 to numRecs do
  314.    begin
  315.       dbx^.StatusUpdate(GenFStatus,i,0);
  316.       BuildRecordData;
  317.       dbx^.Blank;
  318.       dbx^.FieldPut('LASTNAME',gfLastName);
  319.       dbx^.FieldPut('FIRSTNAME',gfFirstName);
  320.       dbx^.FieldPut('STREET',gfStreet);
  321.       dbx^.FieldPut('OFFICE',gfOffice);
  322.       dbx^.FieldPut('CITY',gfCity);
  323.       dbx^.FieldPut('STATE',gfState);
  324.       dbx^.FieldPut('ZIP',gfZip);
  325.       dbx^.FieldPut('TELEPHONE',gfTelephone);
  326.       dbx^.FieldPut('BIRTHDATE',gfBirthDate);
  327.       dbx^.FieldPut('PAYMENT',gfPayment);
  328.       dbx^.FieldPut('PAIDFLAG',gfPaidFlag);
  329.       dbx^.FieldPut('RANDOMNUM',gfRandomNum);
  330.       dbx^.FieldPut('UNIQUEID',gfUniqueID);
  331.       if useMemo then
  332.          dbx^.MemoPut('COMMENTS');
  333.       dbx^.Append;
  334.    end;
  335.    dbx^.StatusUpdate(StatusStop,0,0);
  336.    Dispose(dbx, Done);
  337. end;
  338.  
  339. procedure AddTestData(namObjt : GSP_dBHandler; numRecs : integer);
  340. begin
  341.    dbx := namObjt;
  342.    if CollectionsEmpty then MakeCollections;
  343.    CollectionsEmpty := false;
  344.    useMemo := namObjt^.WithMemo;
  345.    randomize;
  346.    dbx^.StatusUpdate(StatusStart,GenFStatus,numRecs);
  347.    for i := 1 to numRecs do
  348.    begin
  349.       dbx^.StatusUpdate(GenFStatus,i,0);
  350.       BuildRecordData;
  351.       dbx^.Blank;
  352.       dbx^.FieldPut('LASTNAME',gfLastName);
  353.       dbx^.FieldPut('FIRSTNAME',gfFirstName);
  354.       dbx^.FieldPut('STREET',gfStreet);
  355.       dbx^.FieldPut('OFFICE',gfOffice);
  356.       dbx^.FieldPut('CITY',gfCity);
  357.       dbx^.FieldPut('STATE',gfState);
  358.       dbx^.FieldPut('ZIP',gfZip);
  359.       dbx^.FieldPut('TELEPHONE',gfTelephone);
  360.       dbx^.FieldPut('BIRTHDATE',gfBirthDate);
  361.       dbx^.FieldPut('PAYMENT',gfPayment);
  362.       dbx^.FieldPut('PAIDFLAG',gfPaidFlag);
  363.       dbx^.FieldPut('RANDOMNUM',gfRandomNum);
  364.       dbx^.FieldPut('UNIQUEID',gfUniqueID);
  365.       if useMemo then
  366.          dbx^.MemoPut('COMMENTS');
  367.       dbx^.Append;
  368.    end;
  369.    dbx^.StatusUpdate(StatusStop,0,0);
  370. end;
  371.  
  372. end.
  373.